<template>
  <div style="height: 100%">
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="searchForm.roleName" placeholder="角色名" clearable @clear="findByPage"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="findByName" size="medium" icon="el-icon-search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="add" v-if="sys_role_add" size="medium" icon="el-icon-plus">新增</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-tooltip content="刷新" placement="top">
            <el-button icon="el-icon-refresh-right" circle @click="findByPage"></el-button>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <v-table :columns="columns" :data="tableData" :table-operation="tableOperation" :loading="loadingData"
               @findPage="findByPage" @handleClick="handleClick">
        <template slot="operation" slot-scope="scope">
          <el-button
            v-if="sys_role_permission"
            @click="assignPermission(scope.data.row)"
            :size="'mini'"
            :icon="'el-icon-plus'"
            type="text">
            权限
          </el-button>
          <el-button
            v-if="sys_role_edit"
            @click="handleEdit(scope.data.row)"
            :size="editOperation.size || 'mini'"
            :style="{color: editOperation.color|| ''}"
            :icon="editOperation.icon"
            type="text">
            {{editOperation.name}}
          </el-button>
          <el-button
            v-if="sys_role_delete"
            @click="handleDelete(scope.data.row)"
            :size="deleteOperation.size || 'mini'"
            :style="{color: deleteOperation.color|| ''}"
            :icon="deleteOperation.icon"
            type="text">
            {{deleteOperation.name}}
          </el-button>
        </template>
      </v-table>
      <permission-tree-dialog :menu-ids="menuIds"></permission-tree-dialog>
      <el-dialog :title="title" :visible.sync="dialogFormVisible" width="40%">

        <el-form :model="roleForm" label-width="80px" class="demo-ruleForm">
          <el-form-item label="角色名称" prop="roleName" required>
            <el-input v-model="roleForm.roleName" placeholder="请输入角色名称"></el-input>
          </el-form-item>
          <el-form-item label="角色标识" prop="roleCode">
            <el-input v-model="roleForm.roleCode" placeholder="请输入角色标识"></el-input>
          </el-form-item>
          <el-form-item label="角色描述" prop="roleDesc">
            <el-input v-model="roleForm.roleDesc" placeholder="请输入角色描述"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="save" v-if="saveVisible">保 存</el-button>
          <el-button type="primary" @click="update" v-else>更 新</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import vueTable from '../../../components/vueTable.vue'
  import {roleApi} from '../../../http/modules/role'
  import website from "../../../const/website";
  import vTable from '../../../components/table/table'
  import {mapGetters, mapMutations} from 'vuex'
  import PermissionTreeDialog from "./permissionTreeDialog";

  export default {
    name: "role",
    components: {
      PermissionTreeDialog,
      vueTable,
      vTable
    },
    data() {
      return {
        tableData: {
          totalSize: 0,
          content: []
        },
        pageRequest: {
          pageNum: 1,
          pageSize: 10
        },
        columns: [
          {prop: "roleName", label: "角色名称", minWidth: 100},
          {prop: "roleCode", label: "角色码", minWidth: 100},
          {prop: "roleDesc", label: "角色描述", minWidth: 100},
          {prop: "createTime", label: "创建时间", minWidth: 100},
        ],
        tableOperation: {
          operation: true
        },
        // viewOperation: website.viewOperation, // 查看操作
        editOperation: website.editOperation, // 编辑操作
        deleteOperation: website.deleteOperation, // 删除操作
        loadingData: true,
        searchForm: {
          roleName: '',
          pageNum: 0,
          pageSize: 10
        },
        selections: [],
        dialogFormVisible: false,
        saveVisible: true,
        title: '新增',
        roleForm: {
          roleName: '',
          roleCode: '',
          roleDesc: ''
        },
        menuIds: []
      }
    },
    methods: {
      findByPage(data) {
        if (data != null) {
          this.pageRequest = data
        }
        if (this.searchForm.roleName !== '' && data != null) {
          this.searchForm.pageNum = data.pageNum
          this.findByName()
        } else {
          roleApi.findPage(this.pageRequest).then(res => {
            this.tableData = res.data
            this.loadingData = false
          }).catch(error => {
            console.log(error)
            this.$message.error('网络错误')
          })
        }
      },
      findByName() {
        roleApi.findByName(this.searchForm).then(res => {
          this.tableData.totalSize = res.data.length
          this.tableData.content = res.data
        }).catch(error => {
          console.log(error)
          this.$message.error('网络错误，查询失败')
        })
      },
      add() {
        this.dialogFormVisible = true
        this.saveVisible = true
        this.title = '新增'
      },
      cancel() {
        this.dialogFormVisible = false
      },
      save() {
        roleApi.save(this.roleForm).then(res => {
          this.$message.success('新增成功')
          this.dialogFormVisible = false
          this.findByPage()
        }).catch(error => {
          this.$message.error('新增失败')
        })

      },
      handleClick(row, type) {
        let rows = []
        if (type === 'delete') {
          rows.push(row)
          this.handleDelete(rows)
        } else if (type === 'edit') {
          this.handleEdit(row)
        } else if (type === 'view') {
          this.handleView(row)
        } else if (type === 'permission') {
          this.assignPermission(row)
        }
      },
      handleView(data) {

      },
      handleEdit(data) {
        // 修改角色信息
        this.dialogFormVisible = true
        this.saveVisible = false
        this.title = '编辑'
        this.roleForm = data
      },
      update() {
        roleApi.update(this.roleForm).then(res => {
          this.$message.success('更新成功')
        }).catch(error => {
          this.$message.error('更新失败')
        })
        this.dialogFormVisible = false
      },
      handleDelete(rows) {
        // 删除用户
        this.$confirm('此操作将删除该角色，是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delete(rows)
        }).catch(() => {
          this.$message.info('已取消删除')
        })
      },
      delete(datas) {
        let records = []
        datas.forEach(item => {
          records.push({roleId: item.roleId})
        })
        roleApi.delete(records).then(res => {
          this.$message.success('删除成功')
          this.findByPage()
        }).catch(error => {
          this.$message.error('删除失败')
        })
      },

      assignPermission(data) {
        let roleId = data.roleId
        let visible = true
        this.$store.commit('setDialogVisible', {visible, roleId})

        this.findMenuIdByRole(roleId)
      },
      findMenuIdByRole(roleId) {
        roleApi.findMenuIdByRole(roleId).then(res => {
          this.menuIds = res.data
        }).catch(error => {
          console.log(error)
        })
      },

      selectionChange(selections) {
        this.selections = selections
      },
      clearDeptForm() {
        this.roleForm = {
          roleName: '',
          roleCode: '',
          roleDesc: ''
        }
      }
    },
    watch: {
      dialogFormVisible(val, oldValue) {
        if (!val) {
          this.clearDeptForm()
        }
      }
    },
    computed: {
      ...mapGetters(["permissions"])
    },
    created() {
      this.sys_role_view = this.permissions["sys:role:view"]
      this.sys_role_add = this.permissions["sys:role:add"]
      this.sys_role_edit = this.permissions["sys:role:edit"]
      this.sys_role_delete = this.permissions["sys:role:delete"]
      this.sys_role_permission = this.permissions["sys:role:permission"]
      this.tableOperation.operation = this.sys_role_view || this.sys_role_add || this.sys_role_edit ||
        this.sys_role_delete || this.sys_role_permission
    },
    mounted() {
      // this.findByPage(this.pageRequest)
    }
  }
</script>

<style scoped>

</style>
